package com.flower.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-03 10:50
 * 密码加密
 */
public class DruidEncryptUtil {

    private static String publicKey;
    private static String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey:" + privateKey);

            publicKey = keyPair[1];
            System.out.println("publicKey:" + publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }


    public static String encrypt(String password) throws Exception {

        return ConfigTools.encrypt(privateKey, password);
    }



    public static String decrypt(String password) throws Exception {

        return ConfigTools.decrypt(publicKey, password);

    }

    public static void main(String[] args) throws Exception {

        String encrypt = encrypt("123456");
        System.out.println(encrypt);
        System.out.println("---------------");
        String root = encrypt("root");
        System.out.println(root);



    }
}
